<template>
  <div class="container">
    <vxe-toolbar>
      <template v-slot:buttons>
        <vxe-form :data="search" ref="xForm" class="toolbar-form" @submit="loadData" @reset="resetEvent">
         
          <vxe-form-item field="params.keyFlag">
            <template v-slot>
              <vxe-input v-model="search.params.keyFlag" placeholder="资源标识符" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item field="params.displayName">
            <template v-slot>
              <vxe-input v-model="search.params.displayName" placeholder="资源名称" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item style="display: none" field="params.createTime"> </vxe-form-item>
          <vxe-form-item field="startCreateTime">
            <template v-slot>
              <vxe-input type="datetime" v-model="search.startCreateTime" placeholder="开始时间" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item field="endCreateTime">
            <template v-slot>
              <vxe-input type="datetime" v-model="search.endCreateTime" placeholder="结束时间" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item>
            <template v-slot>
              <vxe-button @click="loadData" type="submit" status="primary" size="small">查询</vxe-button>
              <vxe-button type="reset" status="primary" size="small">重置</vxe-button>
              <vxe-button @click="addEvent" status="primary" size="small">新增资源</vxe-button>
            </template>
          </vxe-form-item>
        </vxe-form>
      </template>
    </vxe-toolbar>

    <vxe-table 
    v-loading="loading"
    :data="data"
    height="680px"
    border="none"
    highlight-hover-row
    show-header-overflow
    show-overflow
    align="center"
    :header-cell-style="{background:'#F7F7F8',color:'#606266'}"
    >
      <vxe-table-column title="资源标识符" field="keyFlag"/>
      <vxe-table-column title="父节点" field="parentId" />
      <vxe-table-column title="组件" field="component" />
      <vxe-table-column title="组件名称" field="name" />
      <vxe-table-column title="redirect" field="redirect" />
      <vxe-table-column title="路由路径" field="path" />
      <vxe-table-column title="路径名称" field="displayName" />
      <vxe-table-column title="元数据" field="meta" />
      <vxe-table-column title="备注" field="remark" />
      <vxe-table-column title="创建日期" field="createTime" />
      <vxe-table-column title="禁用" field="available" />
      <vxe-table-column title="类型" field="type" />
      <vxe-table-column title="创建者" field="creator" />
      <vxe-table-column fixed="right" width="250px">
        <template v-slot="{ row }">
          <vxe-button status="primary" icon="fa fa-edit" @click="edit(row)" size="small"></vxe-button>
          <vxe-button status="primary" icon="fa fa-edit" @click="edit(row)" content="权限" size="small"></vxe-button>
          <vxe-button status="danger" icon="fa fa-trash" @click="deleteById(row)" size="small"></vxe-button>
        </template>
      </vxe-table-column>
    </vxe-table>

    <vxe-pager
      @page-change="changePage"
      :current-page.sync="page.page"
      :page-size.sync="page.size"
      :total="page.total"
      :page-sizes="[5, 10, 30, 50, { label: '全量数据', value: -1 }]"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
    >
    </vxe-pager>

    <vxe-modal @close="closeModal" v-model="modalUpdate.show" width="800px" :title="modalUpdate.edit ? '修改&保存' : '新增&保存'">
      <template v-slot>
        <vxe-form :data="modalUpdate.row" title-align="right" title-width="100" @submit="saveOrUpdateModalSubmit">
          <vxe-form-item title="资源标识符" field="keyFlag">
            <template v-slot>
              <vxe-input v-model="modalUpdate.row.keyFlag" placeholder="资源标识符" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="资源名称" field="displayName">
            <template v-slot>
              <vxe-input v-model="modalUpdate.row.displayName" placeholder="资源名称" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="父节点" field="parentId">
            <template v-slot>
              <vxe-input v-model="modalUpdate.row.parentId" placeholder="资源名称" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="组件" field="component">
            <template v-slot>
              <vxe-input v-model="modalUpdate.row.component" placeholder="组件" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="路由路径" field="path">
            <template v-slot>
              <vxe-input v-model="modalUpdate.row.path" placeholder="路由路径" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="路由名称" field="name">
            <template v-slot>
              <vxe-input v-model="modalUpdate.row.name" placeholder="路由名称" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="元数据" field="meta">
            <template v-slot>
              <vxe-input v-model="modalUpdate.row.meta" placeholder="元数据" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="备注信息" field="remark">
            <template v-slot>
              <vxe-input v-model="modalUpdate.row.remark" placeholder="路由备注信息名称" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="类型" field="type">
            <template v-slot>
              <vxe-select v-model="modalUpdate.row.type" placeholder="请选择资源类型" clearable>
                <vxe-option value="0" label="按钮"></vxe-option>
                <vxe-option value="1" label="菜单"></vxe-option>
              </vxe-select>
            </template>
          </vxe-form-item>
          <el-row type="flex" justify="end">
            <el-col :span="5">
              <vxe-form-item>
                <template v-slot>
                  <vxe-button type="submit" status="primary">提交</vxe-button>
                  <vxe-button @click="resetModal" status="primary">重置</vxe-button>
                </template>
              </vxe-form-item>
            </el-col>
          </el-row>
        </vxe-form>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
import { queryByPaging, saveOrUpdate, deleteById } from './tools';
export default {
  name: 'ResourceManagement',
  data() {
    return {
      page: {
        page: 1,
        size: 5,
        total: 0,
      },
      search: {
        params: {
          id: '',
          keyTag: '',
          displayName: '',
          createTime: '',
        },
        startCreateTime: '',
        endCreateTime: '',
      },
      modalUpdate: {
        show: false,
        edit: false,
        row: {},
        originRow: {},
      },
      data: [],
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    isEditing() {
      return this.modalUpdate.edit;
    },
    isIncrease() {
      return !this.modalUpdate.edit;
    },
    saveOrUpdateModalSubmit() {
      const that = this;
      saveOrUpdate(this.modalUpdate.row)
        .then(function (data) {
          if (that.isEditing()) {
            const index = that.data.findIndex((ele) => {
              return ele.id == that.modalUpdate.originRow.id;
            });
            if (index >= 0) {
              that.data.splice(index, 1, that.modalUpdate.row);
            }
          }
          if (that.isIncrease()) {
            that.data.push(data.data);
          }
          that.closeModal();
        })
        .catch(function (error) {
          that.closeModal();
        });
    },
    editPermissions(row) {},
    deleteById(row) {
      this.$XModal.confirm(`确定删除${row.displayName}吗`).then((type) => {
        if (type === 'confirm') {
          deleteById(row.id)
            .then((data) => {
              const index = this.data.findIndex((ele) => {
                return ele.id == row.id;
              });
              if (index >= 0) {
                this.data.splice(index, 1);
                this.$XModal.message('删除成功');
              }
            })
            .catch((err) => {
              console.error('删除用户失败');
            });
        }
      });
    },
    resetEvent() {
      this.loadData();
    },
    changePage() {
      this.loadData();
    },
    edit(row) {
      this.modalUpdate.row = { ...row };
      this.modalUpdate.originRow = { ...row };
      this.modalUpdate.show = true;
      this.modalUpdate.edit = true;
    },
    addEvent() {
      this.modalUpdate.show = true;
      this.modalUpdate.edit = false;
    },
    resetModal() {
      const resetRow = this.modalUpdate.originRow ? this.modalUpdate.originRow : {};
      this.modalUpdate.row = { ...resetRow };
    },
    closeModal() {
      this.modalUpdate.row = {};
      this.modalUpdate.originRow = {};
      this.modalUpdate.show = false;
      this.modalUpdate.edit = false;
    },
    loadData() {
      queryByPaging(this.page.page, this.page.size, this.search.params)
        .then((data) => {
          this.data = data.data;
          this.page.total = data.total;
          console.log('响应数据:%o', data);
        })
        .catch((error) => {
          console.error('请求用户数据发生异常:' + error);
        });
    },
  },
  watch: {
    'search.startCreateTime': function (newVal, oldVal) {
      const start = this.search.startCreateTime ? this.search.startCreateTime : '';
      const end = this.search.endCreateTime ? this.search.endCreateTime : '';
      this.search.params.createTime = start + '@' + end;
    },
    'search.endCreateTime': function (newVal, oldVal) {
      const start = this.search.startCreateTime ? this.search.startCreateTime : '';
      const end = this.search.endCreateTime ? this.search.endCreateTime : '';
      this.search.params.createTime = start + '@' + end;
    },
  },
};
</script>

<style lang="scss" scoped>
.toolbar-form {
  padding-top: 10px;
  padding-left: 10px;
}

.vxe-table {
  margin-left: 10px;
}

//修改toolbar的高度固定的问题
.vxe-toolbar {
  height: auto;
}
</style>
